<?php
/**
 * 会员数据查询
 */
namespace app\common\query;

use app\common\model\Member as MemberModel;
use think\Db;

use app\common\cache\CacheData;

class Member
{
    private $error = '';
    static $status_remark = [
            0 => '临时会员',
            1 => '可用',
            2 => '待审核',
            3 => '禁用',
            4 => '冻结'
        ];
    static $is_agent_remark = [
            1 => '是',
            2 => '否'
        ];
    static $area_remark = [
            1 => 'A区',
            2 => 'B区'
        ];
    static $type_remark = [
            1 => '空单',
            2 => '实单'
        ];
    static $is_phone_checked_remark = [
            0 => '未验证',
            1 => '已验证'
        ];

    public function getError(){
        return $this->error;
    }

    public static function status($key=null){
        if(is_null($key)){
            return self::$status_remark;
        }else{
            return self::$status_remark[$key];
        }
    }
    public static function is_agent($key=null){
        if(is_null($key)){
            return self::$is_agent_remark;
        }else{
            return self::$is_agent_remark[$key];
        }
    }
    public static function area($key=null){
        if(is_null($key)){
            return self::$area_remark;
        }else{
            return self::$area_remark[$key];
        }
    }
    public static function type($key=null){
        if(is_null($key)){
            return self::$type_remark;
        }else{
            return self::$type_remark[$key];
        }
    }
    public static function is_phone_checked($key=null){
        if(is_null($key)){
            return self::$is_phone_checked_remark;
        }else{
            return self::$is_phone_checked_remark[$key];
        }
    }

    /**
     * 获取分页列表
     * @param  [type] $where    [description]
     * @param  string $queryStr [description]
     * @param  string $baseUrl  [description]
     * @return [type]           [description]
     */
    public function getRows($where=[],$queryStr='',$baseUrl=''){
        if(!$baseUrl){
            $baseUrl = url('member.show/index');
        }
        // 每页数量
        $perpagenum = Option::get('perpage_member_num')?Option::get('perpage_member_num'):15;

        $rows = $this->getDatas($where,[],$perpagenum);

        $total = $rows['total'];
        $page = $rows['current_page'];
        $rows['page_html'] = pagination($total, $perpagenum, $page, $baseUrl, $queryStr);

        return $rows;
    }

    /**
     * 查询多条数据
     * @return [type] [description]
     */
	public function getDatas($where=[],$fields=[],$perpagenum=null){
        $model = new MemberModel;
        if($perpagenum){
            $data = $model->where($where)->order('create_time','desc')->field($fields)->paginate($perpagenum);
            $data = $data->toArray();
            $rows = $data['data'];
            foreach($rows as &$row):
                $row = $this->setRemark($row);
            endforeach;
            $data['data'] = $rows;
        }else{
            $data = $model->where($where)->order('create_time','desc')->field($fields)->select();
            $data = $data->toArray();
            foreach($data as &$row):
                $row = $this->setRemark($row);
            endforeach;
        }

        return $data;
	}
    /**
     * 查询单条数据
     * @param  [type] $mid    [description]
     * @param  [type] $fields [description]
     * @return [type]         [description]
     */
    public function getOneData($where,$fields=[]){
        $model = new MemberModel;
        $row = $model->where($where)->field($fields)->find();
        if(!$row){
            $this->error='会员不存在';
            return false;
        }
        $row = $row->toArray();
        $row = $this->setRemark($row);
        // 会员账户
        $memberCoinQuery = new MemberCoin;
        $row['account'] = $memberCoinQuery->getOneData($row['mid']);
        // 大区结余
        $memberAreaScoreQuery = new MemberAreaScore;
        $row['jieyu'] = $memberAreaScoreQuery->getOneMemberAreaScore($row['mid']);
        return $row;
    }
    /**
     * 获取登录用hug
     * @return [type] [description]
     */
    public function getLoginMember($loginname){
        $model = new MemberModel;
        $row = $model->where(['mid'=>$loginname])->whereOr(['phone'=>$loginname])->find();
        if(!$row){
            $this->error='会员不存在';
            return false;
        }
        $row = $row->toArray();
        $row = $this->setRemark($row);
        // 会员账户
        $memberCoinQuery = new MemberCoin;
        $row['account'] = $memberCoinQuery->getOneData($row['mid']);
        // 大区结余
        $memberAreaScoreQuery = new MemberAreaScore;
        $row['jieyu'] = $memberAreaScoreQuery->getOneMemberAreaScore($row['mid']);
        return $row;
    }

    /**
     * 处理字段数据
     * @param [type] $row [description]
     */
    public function setRemark($row){
        if(isset($row['init_level'])){
            // 初始等级
            $initLevel = Level::levelData($row['init_level']);
            $row['init_level_name'] = $initLevel['level_name'];
        }
        if(isset($row['level_id'])){
            // 当前等级
            $level = Level::levelData($row['level_id']);
            $row['level_name'] = $level['level_name'];
        }
        if(isset($row['agent_type_id'])){
            // 代理类型
            $row['agent_type_name'] = $row['agent_type_id']?AgentType::agentTypeData($row['agent_type_id'])['type_name']:'';
        }
        if(isset($row['is_agent'])){
            // 是否是代理
            $row['is_agent_remark'] = $row['is_agent']==1?'是':'否';
        }
        if(isset($row['area'])){
            // 所在大区
            $row['area_remark'] = $row['area']==1?'A区':'B区';
        }
        if(isset($row['type'])){
            // 会员类型
            $row['type_remark'] = $row['type']==1?'空单':'实单';
        }
        if(isset($row['status'])){
            // 会员状态
            $row['status_remark'] = self::status($row['status']);
        }
        if(isset($row['seter_route'])){
            // 会员层级
            $row['floor'] = $row['seter_route']?sizeof(unserialize($row['seter_route']))+1:1;
        }
        if(isset($row['is_phone_checked'])){
            // 是否已验证手机号码
            $row['is_phone_checked_remark'] = $row['is_phone_checked']==1?'已验证':'未验证';
        }
        if(isset($row['city'])){
            // 地址处理
            $code = $row['city'];
            // 省
            $row['address_province'] = substr($row['city'], 0,strlen($row['city'])-4);
            // 市
            $row['address_city'] = substr($row['city'], 0,strlen($row['city'])-2);
            // 县
            $row['address_county'] = $row['city'];
        }
        return $row;
    }

    /**
     * 获取单个字段
     * @param  [type] $where [description]
     * @param  [type] $field [description]
     * @return [type]        [description]
     */
    public function getOneValue($where,$field){
        $model = new MemberModel;
        $data = $model->where($where)->value($field);
        return $data;
    }

    /**
     * 新增会员
     * @param [type] $data [description]
     */
    public function addOneData($data){
        $model = new MemberModel;

        // 是否抵扣
        $is_offset = isset($data['is_offset']) ? $data['is_offset']: 0;

        $seter = $model->where('mid',$data['seter_id'])->find();
        $memberData = $this->initData($data, $seter);

        $levelModel = new \app\common\model\Level;
        // 空单
        if($data['type'] == 1) {
            // 添加会员
            $model->allowField(true)->save($memberData);
            // 处理安置人路径
            $this->setSeters($model->id,$model->mid,$model->seter_id,$model->area);
            // 处理推荐人路径
            $this->setInviters($model->id,$model->mid,$model->inviter_id);
            // 初始化会员账户
            $this->initAccount($model->mid,$model->level_id,$model->type);
            // 会员数量统计
            $levelModel->where(['id'=>$data['level_id']])->setInc('member_count');
            $CACHE = \app\common\cache\CacheData::getInstance('Level');
            $CACHE -> updateCache();
            return true;
        }
        $model->startTrans();
        try {
            // 添加会员
            $model->allowField(true)->save($memberData);

            // 处理安置人路径
            $this->setSeters($model->id,$model->mid,$model->seter_id,$model->area);
            // 处理推荐人路径
            $this->setInviters($model->id,$model->mid,$model->inviter_id);

            // 激活
            if(false===$this->active($model->mid,$model->agent_id,$model->level_id)){
                exception($this->error, 100006);
            }

            // 初始化会员账户
            $this->initAccount($model->mid,$model->level_id,$model->type);
            
            $score = Level::levelData($model->level_id)['score'];
            // 更新状态
            $model->save(['status'=>1, 'score_own'=>$score],['mid'=>$model->mid]);
            // 业绩记录
            $memberScoreQuery = new MemberScore;
            $scoreData = ['mid'=>$model->mid,'score'=>$score,'type'=>'register'];
            $score_model = $memberScoreQuery -> saveOneData($scoreData);
            $scoreId = $score_model->id;
            
            // 发布消息队列
            $actionData = [
                    'mid'=>$model->mid,
                    'score'=>$score,
                    'level_id'=>$model->level_id,
                    'agent_id'=>$model->agent_id
                ];
            $this->publishActions($actionData);

            // 会员数量统计
            $levelModel->where(['id'=>$data['level_id']])->setInc('member_count');
            $CACHE = \app\common\cache\CacheData::getInstance('Level');
            $CACHE -> updateCache();
            
            $model->commit();
        } catch (\MemberException $e) {
            $this->error = $e->getMessage();
            $model->rollback();
            return false;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $model->rollback();
            return false;
        }
        return $scoreId;

    }
    /**
     * 会员激活
     * @param  [type] $mid      [description]
     * @param  [type] $agent_id [description]
     * @param  [type] $level_id [description]
     * @return [type]           [description]
     */
    public function active($mid,$agent_id,$level_id){
        $model = new MemberModel;
        $agent_type_id = $model->where(['mid'=>$agent_id])->value('agent_type_id');
        $configData = AgentTypeLevelCoinConfig::agentTypeConfig($agent_type_id);
        $levelConfig = $configData[$level_id];
        $config = $levelConfig['config'];

        $memberCoinModel = new \app\common\model\MemberCoin;
        // 获取代理中心账户余额
        $agentAccount = $memberCoinModel->where(['mid'=>$agent_id])->select()->toArray();
        $agentAccount= array_combine(array_column($agentAccount,'coin_id'),$agentAccount);

        // 获取等级金额
        $levelAmount = \app\common\query\Level::levelData($level_id)['amount'];

        $memberCoinRecordModel = new \app\common\model\MemberCoinRecord;
        
        foreach($config as $conf):
            // 判断代理中心扣币是否充足
            $decreaceAmount = $levelAmount*$conf['config']/100;
            
            if($decreaceAmount>$agentAccount[$conf['coin_id']]['amount']){
                $this->error='代理中心'.$agent_id.'的'.$conf['coin_name'].'不足';
                return false;
            }
            $where = ['mid'=>$agent_id,'coin_id'=>$conf['coin_id']];
            $beforeAmount = $memberCoinModel->where($where)->value('amount');
            // 扣除代理中心货币
            $memberCoinModel->where($where)->setDec('amount',$decreaceAmount);
            $afterAmount = $memberCoinModel->where($where)->value('amount');
            // 资金变动记录
            $record[] = [
                'operate_id' => 0,
                'mid' => $agent_id,
                'coin_id' => $conf['coin_id'],
                'amount' => $decreaceAmount,
                'operate' => __FUNCTION__,
                'before_amount' => $beforeAmount,
                'after_amount' => $afterAmount,
                'remark' => '激活【'.$mid.'】扣币',
                'flag' => 'dec'
            ];
        endforeach;

        $memberCoinRecordModel->saveAll($record);
        return true;

    }

    /**
     * 初始化会员数据
     * @param $memberData
     * @param $seter
     * @return mixed
     */
    public function initData($data, $seter)
    {
        // 用户状态，空单可用，实单待审核
        $data['status'] = $data['type']==1?1:2;
        // 初始等级
        $data['init_level'] = $data['level_id'];
        // 密码加密
        $data['password'] = hashPwd($data['password']);
        // 交易密码加密
        $data['safe_password'] = hashPwd($data['safe_password']);
        return $data;
    }

    /**
     * 处理安置人路径
     * @param [type] $uid      [description]
     * @param [type] $mid      [description]
     * @param [type] $seter_id [description]
     */
    private function setSeters($uid,$mid,$seter_id,$area){
        // 处理会员安置路径
        $memberSeterModel = new \app\common\model\MemberSeter;
        $seter_seters = $memberSeterModel->where(['mid'=>$seter_id])->select();
        if(!$seter_seters){
            
            // 如果安置人的安置人路径为空，则安置人为顶级用户
            $my_seters = [
                [
                    'uid' => $uid,
                    'mid' => $mid,
                    'index' => 0,
                    'seter_mid' => $seter_id,
                    'area' => $area
                ]
            ];

        }else{
            $seter_seters = $seter_seters->toArray();
            // 否则，在安置人路径基础上，加一条安置人本身
            $seter_seters_num = sizeof($seter_seters);
            $my_seters = [];
            foreach($seter_seters as $seter):
                $seter['mid'] = $mid;
                $seter['uid'] = $uid;
                unset($seter['id']);
                $my_seters[]=$seter;
            endforeach;
            $my_seters[] = [
                'mid' => $mid,
                'uid' => $uid,
                'index' => $seter_seters_num,
                'seter_mid' => $seter_id,
                'area' => $area
            ];

        }
        $memberSeterModel->insertAll($my_seters);
    }

    /**
     * 处理推荐人路径
     * @param [type] $uid      [description]
     * @param [type] $mid      [description]
     * @param [type] $seter_id [description]
     */
    private function setInviters($uid,$mid,$inviter_id){
        // 处理会员推荐路径
        $memberInviterModel = new \app\common\model\MemberInviter;
        $inviter_inviters = $memberInviterModel->where(['mid'=>$inviter_id])->select();
        if(!$inviter_inviters){
            // 如果推荐人的推荐人路径为空，则推荐人为顶级用户
            $my_inviters = [
                [
                    'uid' => $uid,
                    'mid' => $mid,
                    'index' => 0,
                    'inviter_mid' => $inviter_id,
                    'floor' => 0,
                ]
            ];
        }else{
            $inviter_inviters = $inviter_inviters->toArray();
            // 否则，在推荐人路径基础上，加一条推荐人本身
            $inviter_inviters_num = sizeof($inviter_inviters);
            $my_inviters = [];
            foreach($inviter_inviters as $inviter):
                $inviter['mid'] = $mid;
                $inviter['uid'] = $uid;
                $inviter['floor'] += 1;
                unset($inviter['id']);
                $my_inviters[]=$inviter;
            endforeach;
            $my_inviters[] = [
                'mid' => $mid,
                'uid' => $uid,
                'index' => $inviter_inviters_num,
                'inviter_mid' => $inviter_id,
                'floor'=>0,
            ];
        }
        $memberInviterModel->insertAll($my_inviters);
    }

    /**
     * 初始化账户资产
     * @param  [type] $mid [description]
     * @return [type]      [description]
     */
    private function initAccount($mid,$level_id,$type){
        $coinData = Coin::coinData();
        // 所有货币类型金额初始化为0
        $defaultAccount = [];
        foreach($coinData as $coin):
            $defaultAccount[] = [
                'mid' => $mid,
                'coin_id' => $coin['id'],
                'amount' => 0,
            ];
        endforeach;
        $memberCoinQuery = new MemberCoin;
        $memberCoinQuery -> saveDatas($defaultAccount);

        // 初始化AB大区剩余业绩，都为0
        $defaultAreaScore = [
            ['mid'=>$mid,'area'=>1,'score'=>0],
            ['mid'=>$mid,'area'=>2,'score'=>0]
        ];
        $memberAreaQuery = new MemberAreaScore;
        $memberAreaQuery -> saveDatas($defaultAreaScore);

        // 如果是实单
        if($type==2):
            // 读取注册获取配置
            $levelCoinConfig = LevelCoinConfig::levelCoinConfigByType('register');
            // 等级为用户等级的注册获取配置
            foreach($levelCoinConfig as $config):
                if($config['level_id']==$level_id){
                    $data[] = [
                        'mid'=>$mid,
                        'coin_id'=>$config['coin_id'],
                        'amount'=>$config['config']
                    ];
                }
            endforeach;
            $memberCoinQuery -> saveDatas($data);
        endif;
    }

    

    /**
     * 更新会员资料
     * @return [type] [description]
     */
    public function saveOneData($id,$data){
        $model = new MemberModel;
        //dump($data);die;
        return $model -> allowField(true) -> save($data,['id'=>$id]);
    }

    public function saveDatas($datas){
        $model = new MemberModel;
        return $model -> saveAll($datas);
    }

    /**
     * 通过主键删除单个会员
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function deleteOneData($id){
        if($id==1){
            $this->error = '无法删除顶级用户';
            return false;
        }
        // 先将状态变为冻结，再删除
        $model = new MemberModel;
        $model->where('id',$id)->update(['status'=>4]);
        return MemberModel::destroy($id);
    }

	public function queryMap($mid){
        $fields = ['mid','type','status','mname','phone','level_id','area','score_total','seter_id','seter_route'];
    	$topMember = $this->getOneData(['mid'=>$mid],$fields);
    	$memberTree = $topMember;

    	// 上翻
    	$memberTree['up'] = $memberTree['seter_id'];
        $memberTree['down'] = '';

    	$memberTree['son'] = [];
        // 查询下级一层会员
    	$son = $this->memberSon($mid,$fields);
        //dump($son);
    	if($son):
    		foreach($son as $k => $s):
    			// 上翻,上层安置人的安置人
    			$s_seters = $s['seters'];
    			if(sizeof($s_seters)>=2){
                    // 上级超过两层可以上翻
    				$s['up'] = $memberTree['seter_id'];
    			}else{
    				$s['up'] = '';
    			}
    			// 下翻，本身
                $s['down'] = $s['mid'];

    			$memberTree['son'][$k] = $s;
    			$memberTree['son'][$k]['son'] = [];
    			$sonson = $this->memberSon($s['mid'],$fields);
    			if($sonson):
    				foreach($sonson as $kk => $ss):
    					// 上翻
    					$ss_seters = $ss['seters'];
		    			if(sizeof($ss_seters)>=3){
                            // 上级超过3层可以上翻
		    				$ss['up'] = $memberTree['seter_id'];
		    			}else{
		    				$ss['up'] = '';
		    			}
		    			// 下翻
		    			$ss['down'] = $s['mid'];
    					$memberTree['son'][$k]['son'][$kk] = $ss;
    				endforeach;
                else:
                    $memberTree['son'][$k]['son'] = [];
    			endif;
    		
    		endforeach;
        else:
            $memberTree['son'] = [];
    	endif;

		return $memberTree;
    }

    public function memberSon($mid,$fields){
        $model = new MemberModel;
        $memberSeterQuery = new MemberSeter;
        $memberAreaScoreQuery = new MemberAreaScore;
        $data = $model->where(['seter_id'=>$mid])->field($fields)->select();
        if($data){
            $data = $data->toArray();
            foreach($data as &$value):
                // 查询安置路径
                $value['seters'] = $memberSeterQuery->getMemberSeters($value['mid']);
                // 大区结余
                $value['jieyu'] = $memberAreaScoreQuery->getOneMemberAreaScore($value['mid']);
                $value = $this->setRemark($value);
            endforeach;
        }else{
            $data = [];
        }
        return $data;
    }

    /**
     * 原点升级
     * @param  [type] $mid         [description]
     * @param  [type] $agent_id    [description]
     * @param  [type] $level_id    [description]
     * @param  [type] $up_level_id [description]
     * @return [type]              [description]
     */
    public function upgrade($mid,$agent_id,$level_id,$up_level_id){
        // 当前等级业绩
        $level = Level::levelData($level_id);
        $level_score = $level['score'];
        // 目标等级业绩
        $up_level = Level::levelData($up_level_id);
        $up_level_score = $up_level['score'];
        // 该会员所在代理中心类型
        $memberModel = new MemberModel;
        $agent_type_id = $memberModel->where(['mid'=>$agent_id])->value('agent_type_id');
        // 该代理中心在当前等级的激活配置
        $activeConfig = AgentTypeLevelCoinConfig::agentTypeConfig($agent_type_id);
        $levelConfig = $activeConfig[$level_id];
        $levelConfig = $levelConfig['config'];
        $levelConfig = array_combine(array_column($levelConfig, 'coin_id'),$levelConfig);
        $levelCoins = array_keys($levelConfig);
        // 该代理中心在目标等级的激活配置
        $upLevelConfig = $activeConfig[$up_level_id];
        $upLevelConfig = $upLevelConfig['config'];
        $upLevelConfig = array_combine(array_column($upLevelConfig, 'coin_id'),$upLevelConfig);
        $uplevelCoins = array_keys($upLevelConfig);
        // 判断货币配置结构是否一致
        sort($levelCoins);
        sort($uplevelCoins);
        if($levelCoins != $uplevelCoins){
            $this->error = '当前等级与目标等级的货币激活配置结构不一致，升级失败';
            return false;
        }
        // 待扣币计算
        $waitDecAmount = [];
        foreach($upLevelConfig as $config):
            $waitDecAmount[$config['coin_id']] = $up_level_score*$config['config']/100-$level_score*$levelConfig[$config['coin_id']]['config']/100;
        endforeach;

        // 执行扣币与升级
        $memberCoinQuery = new MemberCoin;
        $account = $memberCoinQuery->coinAccount($mid);
        $account = array_combine(array_column($account, 'coin_id'),$account);

        $memberCoinModel = new \app\common\model\MemberCoin;
        $memberCoinModel -> startTrans();
        try{
            // 升级记录
            $upgradeModel = new \app\common\model\OperateUpgrade;
            $upgradeData = [
                'mid' => $mid,
                'level_id' => $level_id,
                'up_level_id' => $up_level_id
            ];
            $upgradeModel -> save($upgradeData);
            $operate_id = $upgradeModel->getLastInsID();

            // 资金变动记录
            $records = [];
            // 该会员余额判断与扣币
            foreach($waitDecAmount as $key => $value):
                if($account[$key]['amount']<$value){
                    exception($account[$key]['coin_name'].'不足，升级失败');
                }
                $beforeAmount = $memberCoinModel -> where(['mid'=>$mid,'coin_id'=>$key])->value('amount');
                $memberCoinModel -> where(['mid'=>$mid,'coin_id'=>$key]) -> setDec('amount',$value);
                $afterAmount = $memberCoinModel -> where(['mid'=>$mid,'coin_id'=>$key])->value('amount');

                // 资金变动记录
                $records[] = [
                    'operate_id' => $operate_id,
                    'mid' => $mid,
                    'coin_id' => $key,
                    'amount' => $value,
                    'operate' => __FUNCTION__,
                    'before_amount' => $beforeAmount,
                    'after_amount' => $afterAmount,
                    'remark' => '['.$mid.']从['.$level['level_name'].']升级到['.$up_level['level_name'].']扣币',
                    'flag' => 'dec'
                ];
            endforeach;
            // 保存资金变动记录
            $memberCoinRecordModel = new \app\common\model\MemberCoinRecord;
            $memberCoinRecordModel->saveAll($records);
            // 新增系统业绩
            $score = $up_level_score-$level_score;
            $score_own = $up_level_score;
            // 更新会员表个人业绩，开启全体分红与加权分红
            $memberModel->where(['mid'=>$mid])->update(['score_own'=>$up_level_score,'is_world'=>1,'is_share'=>1]);
            // 业绩记录
            $memberScoreQuery = new MemberScore;
            $scoreData = ['mid'=>$mid,'score'=>$score,'type'=>'upgrade'];
            $score_model = $memberScoreQuery -> saveOneData($scoreData);
            $scoreId = $score_model->id;
            
            // 发布消息队列
            $actionData = [
                    'mid'=>$mid,
                    'score'=>$score,
                    'level_id'=>$up_level_id,
                    'agent_id'=>$agent_id
                ];
            $this->publishActions($actionData);

            // 变更用户等级
            $memberModel -> where('mid',$mid) -> update(['level_id'=>$up_level_id]);
            // 变更升级记录状态
            $upgradeModel -> where('id',$operate_id) -> update(['status'=>1]);

            // 会员数量统计
            $levelModel = new \app\common\model\Level;
            $levelModel -> where(['id'=>$level_id])->setDec('member_count');
            $levelModel -> where(['id'=>$up_level_id])->setInc('member_count');
            $CACHE = \app\common\cache\CacheData::getInstance('Level');
            $CACHE -> updateCache();
            
            $memberCoinModel->commit();
        }catch(\Exception $e){
            $this->error = $e->getMessage();
            $memberCoinModel->rollback();
            return false;
        }
        return true;
    }

    private function publishActions($actionData){
        // 发布消息队列
        $bonusActionModel = new \app\common\model\BonusAction;
        $actions = [
            [
                'operate_id' => $scoreId,
                'action_name' => 'lingshou',
                'action_data' => serialize($actionData),
            ],
            [
                'operate_id' => $scoreId,
                'action_name' => 'xiaoshou',
                'action_data' => serialize($actionData),
            ],
            [
                'operate_id' => $scoreId,
                'action_name' => 'fuwu',
                'action_data' => serialize($actionData),
            ],
            [
                'operate_id' => $scoreId,
                'action_name' => 'score',
                'action_data' => serialize($actionData),
            ]
        ];
        $bonusActionModel->saveAll($actions);
    }

    public function queryMemberCoin($mid)
    {
        return Db::member('member_coin')->where('mid', $mid)->select();
    }

    public static function ispayPwd($mid='',$paypwd=''){
        $model = new MemberModel;
        $data = $model->where(['mid'=>$mid])->field('safe_password')->find();
        return password_verify($paypwd,$data['safe_password']);
    }

}